package com.jin.webvideo.entity;

import com.jin.webvideo.base.util.SystemInfo;
import lombok.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class SessionStore {
    public static final String VerificationCode = "vCode";
    private User user;
    @Setter(AccessLevel.NONE)
    @Getter(AccessLevel.NONE)
    private Map<String, Object> store = new HashMap<>();
    @Setter(AccessLevel.NONE)
    private long createTime;
    // 跟踪id
    private String traceId;
    // 禁止访问的信息
    private String address;

    public SessionStore() {
        this.createTime = SystemInfo.now();
    }
    public Object getAttribute(String key) {
        return store.get(key);
    }
    public void setAttribute(String key, Object val) {
        store.put(key, val);
    }
    public void removeAttribute(String key) {
        store.remove(key);
    }

    public List<User.Role> roles() {
        return isAnonymousUser()? Collections.singletonList(User.Role.ROLE_ANONYMOUS) : this.user.getRoles();
    }
    public boolean isAnonymousUser() {
        return this.user == null;
    }

    public static<T> T getAttribute(SessionStore store, String key) {
        return (T) store.getAttribute(key);
    }
}
